package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.HouseListBean;
import com.csweimei.rentmanagementtreasure.http.entity.HouseListEntBean;
import com.csweimei.rentmanagementtreasure.http.entity.QuarterEntity;
import com.csweimei.rentmanagementtreasure.pop.PopBean;
import com.csweimei.rentmanagementtreasure.pop.PopQuarterChoose;
import com.csweimei.rentmanagementtreasure.pop.PopSelClick;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.HouseInfoPVM;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.HouseInfoVM;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class HouseInfoEntVM extends BaseViewModel<Repository> {
    public HouseInfoEntVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    public ObservableField<String> selType = new ObservableField<>("整租");

    public BindingCommand back = new BindingCommand(() -> finish());

    public ObservableField<String> companyid = new ObservableField<>("");
    public ObservableField<String> keyword = new ObservableField<>("");
    public ObservableField<String> other = new ObservableField<>("");
    public ObservableField<String> city = new ObservableField<>("全部");
    public ObservableField<String> quarters = new ObservableField<>("全部");
    public ObservableField<String> housingType = new ObservableField<>("");
    public ObservableField<String> toward = new ObservableField<>("");


    @Override
    public void onResume() {
        super.onResume();
        if (model.isLogin())
            getData();

    }

    public ObservableField<Integer> showNoData = new ObservableField<>(View.VISIBLE);

    public ObservableList<HouseInfoEntP1VM> observableList = new ObservableArrayList<>();
    public ItemBinding<HouseInfoEntP1VM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_house_info_ent_1);

    private void getData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",companyid.get());
        map.put("keyword",keyword.get());
        map.put("category",selType.get());
        map.put("city",city.get());
        map.put("quarters",quarters.get());
        map.put("HouseType",housingType.get());
        map.put("other",other.get());
        map.put("toward",toward.get());

        model.allhousing(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<HouseListEntBean>() {
                    @Override
                    public void onNext(HouseListEntBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size() > 0){
                                showNoData.set(View.GONE);
                                for (HouseListEntBean.DataDTO pBean : o.getData()) {
                                    HouseInfoEntP1VM houseInfoPVM = new HouseInfoEntP1VM(HouseInfoEntVM.this,pBean);
                                    observableList.add(houseInfoPVM);
                                }
                            }else {
                                showNoData.set(View.VISIBLE);
                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNoData.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public BindingCommand showxq = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getQuartersData();
        }
    });

    private void getQuartersData() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        model.get_quarters(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<QuarterEntity>() {
                    @Override
                    public void onNext(QuarterEntity bean) {
                        if (bean.issucc){
                            showPop(bean.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void showPop(List<QuarterEntity.DataDTO> data) {
        List<String> list = new ArrayList<>();
        list.add("全部");
        for (QuarterEntity.DataDTO bean:data){
            list.add(bean.getQuarters());
        }

        PopQuarterChoose popQuarters = new PopQuarterChoose(mActivity, list,new PopSelClick() {
            @Override
            public void sel(PopBean x) {
                quarters.set(x.getName());
                getData();
            }
        });
        PopUtils.showAttachPop(mActivity,view,popQuarters);

    }


    LinearLayout view;
    public void setView(LinearLayout llRoot) {
        view = llRoot;
    }

    public void setHouseStatus(int id, String status){
        Map<String,String> map = new HashMap<>();
        map.put("userid",model.getLoginInfo().getData().getUser_id());
        map.put("sid",""+id);
        map.put("roomid","");
        map.put("status",status);
        model.housingstatic(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult bean) {
                        if (bean.issucc){
                            getData();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
